// pages/products/products.js
import api from '../../api/api'
import { urlObj} from '../../api/url'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList:[],
    productList:[],
    aboutObj:{},
    historyObj:{},
    banner2:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBannerList(1)
    this.getProducts(1)
    this.getProducts(2)
    this.getProducts(3)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  routerGoAboout(path){
    wx.navigateTo({
      url: '../about/about'
    });
  },
  routerGoPro(e){
    wx.navigateTo({
      url: `../products/productList`
    });
  },
  goHistory() {
    wx.navigateTo({
      url: '../history/history'
    });
  },
  // 获取banner图
  getBannerList(type) {
    const params = {
      type:type
    }
    api.get(urlObj.getBannerList, params).then(res => {
      if(res.statusCode == '200') {
        let { items } = res.data
        items.map(item => {
          item.bannerPath =  urlObj.imgHost + item.bannerPath
        });
        console.log(items)
        if(type == 1){
          this.setData({
            bannerList:items
          })
        } else {
          this.setData({
            banner2:items[0]
          })
        }
      }else{
        wx.showToast({
          icon:'none',
          title: res.msg,
        })
      }
    }).catch(err => {
      console.log(err)
    })
  },
  // 获取产品列表
  getProducts(type) {
    const params = {
      page:1,
      limit:50,
      types:type,
      isEnable:true
    }
    api.get(urlObj.getProductList,params).then(res => {
      if(res.statusCode == '200') {
        let { items } = res;
        items.forEach(item => {
          item.picture = urlObj.imgHost + item.picture
        })
        if(type == 1){
          console.log(items)
          this.setData({
            productList:items || []
          })
        }else if(type == 2) {
          this.setData({
            historyObj:items[0]
          })
        }else{
          this.setData({
            aboutObj:items[0]
          })
        }
      }else{
        wx.showToast({
          icon:'none',
          title: res.msg,
        })
      }
    }).catch(err => {
      console.log(err)
    })
  },
  viewDetails(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../productDetail/productDetail?id=${id}`
    });
  },
  // 跳转微信公众号
  openwxPage(){
  },
  goContactPage(){
    wx.navigateTo({
      url: `../about/contact`
    });
  }
})